package com.serkol.ams.controller.api;

import com.serkol.ams.bean.MyPageOld;
import com.serkol.ams.bean.Result;
import com.serkol.ams.entity.Banks;
import com.serkol.ams.entity.LoginLog;
import com.serkol.ams.entity.UsersBank;
import com.serkol.ams.service.BanksService;
import com.serkol.ams.service.UsersBankService;
import com.serkol.ams.service.UsersService;
import com.serkol.ams.service.UsersWalletService;
import com.serkol.annotation.IsLogin;
import com.serkol.constant.AuthConstant;
import com.serkol.constant.ResultCode;
import com.serkol.constant.ResultConstant;
import com.serkol.exception.AuthException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/users/bank")
public class UsersBankApiController {


    @Autowired
    private UsersService userService;
    @Autowired
    private UsersWalletService walletService;

    @Autowired
    private BanksService banksService;
    @Autowired
    private UsersBankService usersBankService;


    @IsLogin
    @GetMapping("/findBanksMiniList")
    @ResponseBody
    public Result findBanksMiniList() {
        List<Map<String, Object>> info = banksService.findBanksMiniList();
        return Result.ok(info);
    }


    //  @IsLogin
    @GetMapping("/findBanksList")
    @ResponseBody
    public Result findBanksList(@RequestParam(name = "pageSize", defaultValue = "10") int pageSize, @RequestParam(name = "pageSize", defaultValue = "0") int current) {
        return banksService.findBanksList(pageSize, current);

    }


    @IsLogin
    @GetMapping("/findByMyBankList")
    @ResponseBody
    public Result findByMyBankList(@RequestParam(name = "pageSize", defaultValue = "10") int pageSize, @RequestParam(name = "current", defaultValue = "0") int current) {

        MyPageOld info = usersBankService.findByPageList(pageSize, current);
        return Result.ok(info);

    }

    @IsLogin
    @GetMapping("/findBanksMyMiniList")
    @ResponseBody
    public Result findBanksMyMiniList() {
        List<Map<String, Object>> info = usersBankService.findBanksMyMiniList();
        return Result.ok(info);
    }

    @IsLogin
    @PostMapping("/myBankCardByDelete")
    @ResponseBody
    public Result myBankCardByDelete(@RequestParam(value = "id", defaultValue = "0") long id) {

        boolean byDelete = usersBankService.byDelete(id);

        return byDelete ? Result.ok() : Result.failure();
    }


    //findByEditMyBank
    @PostMapping("/findByEditMyBank")
    @ResponseBody
    public Result findByEditMyBank(@RequestBody UsersBank usersBank) throws AuthException {
        System.out.println(usersBank);
        LoginLog loginLog = AuthConstant.getLoginLog();
        usersBank.setUserId(loginLog.getUserId());

        boolean b = false;
        if (usersBank.getId() != null) {

            b = usersBankService.updateById(usersBank);

        } else {
            b = usersBankService.save(usersBank);
        }

        if (b) {
            return Result.ok(usersBank);
        } else {
            throw new AuthException(ResultCode.REQUEST_ERROR_NOT_ACCEPTABLE);
        }
    }

    @IsLogin
    @PostMapping("editBank")
    public Result editBank(@RequestBody Banks data) {


        boolean b = false;
        if (data.getId() != null && data.getId() > 0) {
            b = banksService.updateById(data);
        } else {

            b = banksService.save(data);

        }
        return b ? Result.ok() : Result.failure(ResultConstant.FAIL);

    }
    /*
    *
    * 银行卡查询接口
    * */
    @IsLogin
    @GetMapping("/info")

    public Result info(
            @RequestParam(value = "id", defaultValue = "0") long id
    ) {
        LoginLog loginLog = AuthConstant.getLoginLog();
        Map<String, Object> info = usersBankService.getInfo(id);
        return Result.ok(info);
    }
    /*
    * 解绑银行卡
    * @param id
    *
    * */
    @IsLogin
    @PostMapping("/unbind")
    public Result unbind(
            @RequestParam(value = "id", defaultValue = "0") long id
    ) {

        UsersBank usersBank = usersBankService.getById(id);
        if (usersBank == null) {
            return Result.failure(ResultConstant.FAIL);
        }
        usersBank.setState(1);
        boolean b = usersBankService.updateById(usersBank);
        return b? Result.ok() : Result.failure(ResultConstant.FAIL);
    }

}
